#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>

#if defined(LIBXML_TREE_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)

int createPolicy(char* policyName, char* interface[], char* cost, char* location, char* bandwidth, int count, int appID){
	xmlDocPtr doc;
	xmlNodePtr node;
	xmlNodePtr nodeLevel2;
	int i;	
	doc = xmlParseFile("mypolicy.xml");
	//xmlNodePtr root = xmlNewNode(NULL, BAD_CAST "root");
	xmlNodePtr root = doc->children;
	root=root->next;	
	xmlDocSetRootElement(doc, root);

	/*xmlNewChild(root, NULL, BAD_CAST "node1",
                BAD_CAST "content of node 1");
	xmlNewChild(root, NULL, BAD_CAST "node2", NULL);*/
	



	node = xmlNewChild(root, NULL, BAD_CAST "policy", NULL);
	xmlNewProp(node, BAD_CAST "name", BAD_CAST appID);
	xmlNodePtr intSet = xmlNewChild(node, NULL, BAD_CAST "interfaceSet", BAD_CAST NULL);
	for(i=0;i<count;i++)
	{
		xmlNewChild(intSet, NULL, BAD_CAST "interface", BAD_CAST interface[i]);		
	}
	//printf("0\n");
	xmlNewChild(node, NULL, BAD_CAST "cost", BAD_CAST cost);
	//printf("1\n");
	xmlNewChild(node, NULL, BAD_CAST "location", BAD_CAST location);		
	//printf("2\n");	
	xmlNewChild(node, NULL, BAD_CAST "bandwidth", BAD_CAST bandwidth);
	//printf("3\n");
	xmlNewChild(node, NULL, BAD_CAST "appID", BAD_CAST appID);	
	//printf("Okay\n");


	
    /* 
     * Dumping document to stdio or file
     */
    xmlSaveFormatFileEnc("mypolicy.xml", doc, "UTF-8", 1);

    /*free the document */
    xmlFreeDoc(doc);

    /*
     *Free the global variables that may
     *have been allocated by the parser.
     */
    xmlCleanupParser();

    /*
     * this is to debug memory for regression tests
     */
    xmlMemoryDump();
    return(0);

}
#endif
